package com.xinlu.derek.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xinlu.common.log.Logger;

public abstract class BaseAction extends ActionSupport implements ModelDriven<BaseForm> {
	
	/**
	 * 获取请求
	 * @return
	 */
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 获取响应
	 * @return
	 */
	protected HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 获取会话
	 * @return
	 */
	protected HttpSession getSession(){
		return getRequest().getSession();
	}
	

	protected static Logger logger = Logger.getLogger(BaseAction.class);

	// 用来获取request,response,session,application
	private ActionContext actionContext;

	/**
	 * 针对子类Action的操作辅助类
	 */
	private BaseActionUtil actionUtil = new BaseActionUtil(this);

	@Override
	public String execute() throws Exception {
		
		String method = "execute";

		logger.startMethod(method);

		actionContext = ActionContext.getContext();

		BaseForm fr = this.getModel();

		// 页面ID
		String pageId = fr.getPageId();

		// 动作
		String doAction = fr.getDoAction();

		String sForwardName = null;

		// 判断是否该页面
		if (actionUtil.isCurrentPage(pageId)) {

			logger.info("[" + pageId + "] this request is current page");

			// //////////////////////
			// 执行doAction之前的验证
			// //////////////////////

			// 注解验证
			// annotationVaildationUtil.checkAnnotation(this, doAction, fr);

			// 执行doAction之前的验证
			// formVaildationUtil.doCheck(doAction, fr);

			// 执行doAction操作
			sForwardName = actionUtil.doAction(doAction);

		} else {

			sForwardName = SUCCESS;

			logger.info("this request is not current page");
		}

		// 执行doAction后的操作，主要为formInit
		sForwardName = actionUtil.afterTreatment(sForwardName);

		logger.endMethod(method);

		return sForwardName;
	}

	public abstract String doExecute() throws Exception;

	public abstract void formInit();

}
